package com.cytech.cloudclinic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.base.BasePresenter;
import com.cytech.cloudclinic.moudle.login.LoginActivity;
import com.cytech.cloudclinic.moudle.main.clinic.ClinicFragment;
import com.cytech.cloudclinic.moudle.main.drug.DrugManagerFragment;
import com.cytech.cloudclinic.moudle.main.expert.ExportFragment;
import com.cytech.cloudclinic.moudle.main.patient.PatientFragment;
import com.cytech.cloudclinic.moudle.main.setting.SettingFragment;
import com.yechaoa.yutils.SpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.cytech.cloudclinic.app.Constant.USER_TOKEN;

public class MainActivity extends BaseActivity {

    @BindView(R.id.rg_main)
    RadioGroup radioGroup;
    @BindView(R.id.fl_main_container)
    FrameLayout container;
    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment currentFragment;
    private Fragment clinicFragment;
    private Fragment drugFragment;
    private Fragment patientFragment;
    private Fragment expertFragment;
    private Fragment settingFragment;

    public static void actionStart(Context context){
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        String token = SpUtil.getString(USER_TOKEN);
        if (TextUtils.isEmpty(token)) {
            LoginActivity.actionStart(this);
            finish();
            return;
        }
        fragmentList = new ArrayList<>();
        clinicFragment = ClinicFragment.newInstance();
        drugFragment = DrugManagerFragment.newInstance();
        patientFragment = PatientFragment.newInstance();
        expertFragment = ExportFragment.newInstance();
        settingFragment = SettingFragment.newInstance();
        fragmentList.add(clinicFragment);
        fragmentList.add(drugFragment);
        fragmentList.add(patientFragment);
        fragmentList.add(expertFragment);
        fragmentList.add(settingFragment);

        switchFragment(0);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_main_clinic:
                        switchFragment(0);
                        break;
                    case R.id.rb_main_drug:
                        switchFragment(1);
                        break;
                    case R.id.rb_main_patient:
                        switchFragment(2);
                        break;
                    case R.id.rb_main_expert:
                        switchFragment(3);
                        break;
                    case R.id.rb_main_system_setting:
                        switchFragment(4);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void switchFragment(int position) {
        currentFragmentIndex = position;
        Fragment targetFragment = fragmentList.get(position);
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        if (!targetFragment.isAdded()) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.fl_main_container, targetFragment).commit();
            //System.out.println("还没添加呢");
        } else {
            transaction
                    .hide(currentFragment)
                    .show(targetFragment)
                    .commit();

            //System.out.println("添加了( ⊙o⊙ )哇");
        }
        currentFragment = targetFragment;
    }

}